package com.mdd.admin.service.system.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.system.ICouponReceiveService;
import com.mdd.admin.service.system.ISiginLogService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.vo.system.CouponReceiveListVo;
import com.mdd.admin.vo.system.SiginLogListVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.system.CouponReceive;
import com.mdd.common.entity.user.UserSigninLog;
import com.mdd.common.mapper.system.CouponReceiveMapper;
import com.mdd.common.mapper.user.UserSigninLogMapper;
import com.mdd.common.utils.StringUtil;
import com.mdd.common.utils.TimeUtil;
import com.mdd.common.utils.UrlUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 签到记录实现类
 * @author panweiliang
 */
@Service
public class SiginLogServiceImpl implements ISiginLogService {
        
    @Resource
    UserSigninLogMapper userSigninLogMapper;

    /**
     * 签到记录列表
     *
     * @author panweiliang
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return PageResult<SiginLogListVo>
     */
    @Override
    public PageResult<SiginLogListVo> list(PageParam pageParam, Map<String, String> params) {
        Integer page  = pageParam.getPageNo();
        Integer limit = pageParam.getPageSize();

        MPJQueryWrapper<UserSigninLog> mpjQueryWrapper = new MPJQueryWrapper<UserSigninLog>()
                .selectAll(UserSigninLog.class)
                .select("user.nickname, user.avatar, pl.change_points AS changePoints, pl.changeless_points AS changelessPoints, pl.additional_points AS additionalPoints, DATE_FORMAT(t.add_time,'%Y-%m-%d %H:%i:%s') AS addTime ")
                .innerJoin("?_user user ON user.id = t.user_id".replace("?_", GlobalConfig.knowledgePaymentTablePrefixe))
                .innerJoin("?_points_log pl ON pl.owner_id = t.id".replace("?_", GlobalConfig.knowledgePaymentTablePrefixe))
                        .orderByDesc("t.create_time");



        if (StringUtil.isNotEmpty(params.get("keyword"))) {
            String keyword = params.get("keyword");
            mpjQueryWrapper.nested(wq->wq
                    .like("user.sn", keyword).or()
                    .like("user.nickname", keyword).or()
                    .like("user.mobile", keyword));
        }

        IPage<SiginLogListVo> iPage = userSigninLogMapper.selectJoinPage(new Page<>(page, limit), SiginLogListVo.class, mpjQueryWrapper);

        for(SiginLogListVo item : iPage.getRecords()) {
            item.setAvatar(UrlUtil.toAbsoluteUrl(item.getAvatar()));
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

}
